#pragma once

#include "kernel/interfaces/Result.h"

#include <QString>
#include <QVariant>
#include <chrono>
#include <optional>

namespace base::tasking {

/**
 * @brief 任务优先级
 */
enum class TaskPriority {
    Low = 0,
    Normal,
    High,
    Critical
};

/**
 * @brief 任务类别，用于区分读写等场景
 */
enum class TaskCategory {
    Read = 0,
    Write,
    Maintenance
};

/**
 * @brief 重试策略
 */
struct TaskRetryPolicy {
    int maxRetries = 0;
    std::chrono::milliseconds delay{0};
};

/**
 * @brief 任务执行结果
 */
struct TaskResult {
    bool success = true;
    kernel::ErrorCode errorCode = kernel::ErrorCode::Success;
    QString errorMessage;
    QVariant payload;

    static TaskResult successResult()
    {
        return TaskResult{};
    }

    static TaskResult successResult(QVariant payloadValue)
    {
        TaskResult result;
        result.payload = std::move(payloadValue);
        return result;
    }

    static TaskResult failureResult(kernel::ErrorCode code, const QString& message)
    {
        TaskResult result;
        result.success = false;
        result.errorCode = code;
        result.errorMessage = message;
        return result;
    }

    bool hasPayload() const noexcept { return payload.isValid(); }

    template<typename T>
    std::optional<T> payloadAs() const {
        if (!payload.isValid() || !payload.canConvert<T>()) {
            return std::nullopt;
        }
        return payload.value<T>();
    }
};

} // namespace base::tasking
